package com.sa07.ui.dialog;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import com.sa07.bean.UserArgsConstant;
import com.sa07.config.UserArgsConfig;
import com.sa07.exception.SaveConfigFailedException;
import com.sa07.utils.StringUtils;

/**
 * 用户参数设置
 * 
 * @author Long.shangbo
 * 
 *         2014-2-15 上午4:56:13
 */
public class UserArgsConfigDialog extends DefaultConfigDialog {

	private static final long serialVersionUID = -8198610067111132549L;

	private int initWidth = 380;

	private int initHeight = 400;
	
	private JButton saveBtn = null;
	
	private JTextField lowMachineIpTxt = null;
	
	private JTextField lowMachinePortTxt = null;
	
	private JButton defaultBtn = null;
	
	public UserArgsConfigDialog(){
	}
	
	@Override
	protected void initListeners() {
		//回复默认
		defaultBtn.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				int res = JOptionPane.showConfirmDialog(null, 
						"确定要恢复默认配置吗？", "温馨提示", JOptionPane.YES_NO_OPTION);
				
				if(res == JOptionPane.OK_OPTION){
					initValue();
				}
			}
		});
		
		//保存配置
		saveBtn.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				String ip = lowMachineIpTxt.getText().trim();
				String port = lowMachinePortTxt.getText().trim();
				
				if(StringUtils.isNullOrEmpty(ip)){
					JOptionPane.showMessageDialog(null, "下位机IP地址不能为空!");
					return;
				}
				
				if(StringUtils.isNullOrEmpty(port)){
					JOptionPane.showMessageDialog(null, "下位机端口不能为空!");
					return;
				}
				
				UserArgsConfig.INSTANCE.modifyArg(UserArgsConstant.LOWER_MACHINE_IP, ip);
				UserArgsConfig.INSTANCE.modifyArg(UserArgsConstant.LOWER_MACHINE_PORT, port);
				
				try {
					UserArgsConfig.INSTANCE.saveToFileSystem();
					UserArgsConfigDialog.this.dispose();
				} catch (SaveConfigFailedException e1) {
					JOptionPane.showMessageDialog(null, "用户参数配置保存失败!");
					return;
				}
			}
		});
	}

	@Override
	protected void initComponents() {
		int x = 20;
		int y = 20;
		int lblWidth = 90;
		int txtWidth = 220;
		int comHeight = 25;
		int marginTop = 40;
		int marginLeft = x + lblWidth;
		
		JLabel lowMachineIpLbl = new JLabel("下位机IP：");
		lowMachineIpLbl.setBounds(x, y, lblWidth, comHeight);
		add(lowMachineIpLbl);
		
		lowMachineIpTxt = new JTextField();
		lowMachineIpTxt.setBounds(marginLeft, y, txtWidth, comHeight);
		add(lowMachineIpTxt);

		y += marginTop;
		
		JLabel lowMachinePortLbl = new JLabel("下位机端口：");
		lowMachinePortLbl.setBounds(x, y, lblWidth, comHeight);
		add(lowMachinePortLbl);
		
		lowMachinePortTxt = new JTextField();
		lowMachinePortTxt.setBounds(marginLeft, y, txtWidth, comHeight);
		add(lowMachinePortTxt);
		
		int btnWidth = 70;
		int btnMarginTop = initHeight - 80;
		x = initWidth - 200;
		
		defaultBtn = new JButton("恢复默认");
		defaultBtn.setBounds(x, btnMarginTop, btnWidth + 20, comHeight);
		add(defaultBtn);
		
		x += 100;
		saveBtn = new JButton("保存");
		saveBtn.setBounds(x, btnMarginTop, btnWidth, comHeight);
		add(saveBtn);
	}

	@Override
	protected int getWindowWidth() {
		return initWidth;
	}

	@Override
	protected int getWindowHeight() {
		return initHeight;
	}

	@Override
	protected String getWindowTitle() {
		return "用户参数设置";
	}

	@Override
	protected void initValue() {
		lowMachineIpTxt.setText(UserArgsConfig.INSTANCE.getUserArgs(UserArgsConstant.LOWER_MACHINE_IP));
		lowMachinePortTxt.setText(UserArgsConfig.INSTANCE.getUserArgs(UserArgsConstant.LOWER_MACHINE_PORT));
	}
}
